home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / SDKs / Apple Game Sprockets / NetSprocket / NewNSpTest Sources / windows.c < prev   
Encoding:
C/C++ Source or Header  |  1997-09-18  |  8.4 KB  |  387 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************
  2. #
  3. #        Windows.c
  4. #
  5. #        This segment handles the window creation, close, updates,
  6. #
  7. #        Author(s):     Michael Marinkovich
  8. #                    Apple Developer Technical Support
  9. #                    marink@apple.com
  10. #
  11. #        Modification History: 
  12. #
  13. #            2/10/96        MWM     Initial coding                     
  14. #
  15. #        Copyright © 1992-96 Apple Computer, Inc., All Rights Reserved
  16. #
  17. #
  18. #        You may incorporate this sample code into your applications without
  19. #        restriction, though the sample code has been provided "AS IS" and the
  20. #        responsibility for its operation is 100% yours.  However, what you are
  21. #        not permitted to do is to redistribute the source as "DSC Sample Code"
  22. #        after having made changes. If you're going to re-distribute the source,
  23. #        we require that you make it clear in the source that the code was
  24. #        descended from Apple Sample Code, but that you've made changes.
  25. #
  26. *************************************************************************************/
  27.  
  28. #include <Windows.h>
  29. #include <PLStringFuncs.h>
  30.  
  31. #include "App.h"
  32. #include "Proto.h"
  33. #include "netstuff.h"
  34.  
  35. //----------------------------------------------------------------------
  36. //
  37. //    Globals 
  38. //
  39. //----------------------------------------------------------------------
  40.  
  41. extern Boolean        gHasAbout;        // have an about box?
  42. extern short        gWindCount;
  43.  
  44.  
  45. //----------------------------------------------------------------------
  46. //
  47. //    CreateWindow - create a window from the info passed in. Will try to 
  48. //                   load from resource if resID is supplied.
  49. //
  50. //----------------------------------------------------------------------
  51.  
  52. WindowPtr CreateWindow(short resID, void *wStorage, Rect *bounds,  Str255 title,
  53.                         Boolean visible, short procID , short kind,
  54.                         WindowRef behind, Boolean goAwayFlag, long refCon)
  55. {
  56.     OSErr            err = nil;
  57.     WindowRef        newWindow = nil;
  58.     
  59.     
  60.     if (resID != nil)         // if res id isn't nil then load from disk
  61.         newWindow = GetNewCWindow(resID, wStorage, behind);
  62.     else                    // otherwise make a new windowRecord
  63.          newWindow = NewCWindow(wStorage, bounds, title, visible, 
  64.                                procID, behind, goAwayFlag, refCon);
  65.                                
  66.     if (newWindow != nil) 
  67.     {
  68.         NewWindowTitle(newWindow, title);
  69.         err = InitWindowProcs(newWindow, kind);
  70.         
  71.         if (err == noErr) 
  72.         {
  73.             if (visible)
  74.             {
  75.                 SetPort(newWindow);
  76.                 ShowWindow(newWindow);
  77.             }
  78.         }
  79.                 
  80.         // initialization of Document Record faild
  81.         // so kill the return window    
  82.         else 
  83.         {
  84.             newWindow = nil;
  85.             HandleError(err, false);
  86.         }        
  87.     }
  88.     
  89.     return newWindow;
  90.  
  91. }
  92.     
  93.  
  94. //----------------------------------------------------------------------
  95. //
  96. //    RemoveWindow - applications Doc window disposal routine.
  97. //                 
  98. //
  99. //----------------------------------------------------------------------
  100.  
  101. OSErr RemoveWindow( WindowRef window )
  102. {
  103.     OSErr            err = nil;
  104.     short            kind;
  105.     DocHnd            doc;
  106.     
  107.     DoCloseNetWindow(window);
  108.     
  109. //    DoRemoveNetWindow(window);
  110. /*    
  111.     kind = GetWindKind( window );
  112.     if (kind < kDocKind || kind > kAboutKind)
  113.         return -1;         // not our window
  114.     
  115.     doc = (DocHnd)GetWRefCon(window);
  116.     if (doc != nil) 
  117.     {
  118.         switch( kind ) 
  119.         {
  120.             case kDocKind:
  121.                 DisposeWindowStructure(doc);
  122.                 DisposeWindow(window);
  123.                 err = noErr;
  124.                 break;
  125.             
  126.             case kAboutKind:
  127.                 DisposeWindowStructure(doc);
  128.                 DisposeWindow(window);
  129.                 err = noErr;
  130.                 gHasAbout = false;    // allow new about box
  131.                 break;
  132.             
  133.             default:
  134.                 break;
  135.                     
  136.  
  137.         }    
  138.     }
  139. */    
  140.     return err;
  141. }        
  142.  
  143.  
  144. //----------------------------------------------------------------------
  145. //
  146. //    DisposeWindowStructure - dispose of Document Record prior to
  147. //                              releasing the window.
  148. //
  149. //----------------------------------------------------------------------
  150.  
  151. void DisposeWindowStructure(DocHnd doc)
  152. {    
  153.     if (doc != nil)
  154.     {
  155.         HLock((Handle) doc);
  156.         
  157.         // go through the fields of the DocHnd and 
  158.         // dispose of memory that we allocated.
  159.         if ((**doc).hScroll)
  160.             DisposeControl((**doc).hScroll);
  161.     
  162.         if ((**doc).vScroll)
  163.             DisposeControl((**doc).vScroll);
  164.             
  165.         if ((**doc).world)
  166.             DisposeGWorld((**doc).world);
  167.         
  168.         if ((**doc).pict)
  169.             KillPicture((**doc).pict);
  170.         
  171.         HUnlock((Handle) doc);
  172.         DisposeHandle((Handle) doc);
  173.     }
  174.     
  175.  
  176. }
  177.  
  178.  
  179. //----------------------------------------------------------------------
  180. //
  181. //    NewWindowTitle - if supplied title is nil then title is set to 
  182. //                      global "gWindCount".
  183. //
  184. //----------------------------------------------------------------------
  185.  
  186. void NewWindowTitle(WindowRef window, Str255 str)
  187. {
  188.     Str255        catStr = "\pConnection ";
  189.     Str255        newStr;
  190.     
  191.     
  192.     if (str == nil || StrLength(str) == 0) 
  193.     {
  194.         PLstrcpy(newStr, catStr);
  195.         NumToString(gWindCount, catStr);
  196.         PLstrcat(newStr, catStr);
  197.         gWindCount++;
  198.         
  199.         SetWTitle(window,newStr);
  200.     }
  201.     
  202.     else
  203.         SetWTitle(window, str);
  204.  
  205. }
  206.  
  207.  
  208. //----------------------------------------------------------------------
  209. //
  210. //    InitWindowProcs - init a window with proper callback event. Fills 
  211. //                       out custom procs for different windowkinds.
  212. //
  213. //----------------------------------------------------------------------
  214.  
  215. OSErr InitWindowProcs(WindowRef window, short windKind)
  216. {
  217.     OSErr            err = nil;
  218.     DocHnd            doc;
  219.     
  220.     doc = (DocHnd)NewHandle(sizeof(DocRec));
  221.     if (doc != nil) 
  222.     {
  223.         SetWRefCon(window, (long)doc);
  224.  
  225.         switch(windKind) 
  226.         {
  227.             case kDocKind:
  228.                 (**doc).idleProc        = DoIdle;
  229.                 (**doc).mMenuProc        = HandleMenuChoice;
  230.                 (**doc).inContentProc    = HandleContentClick;
  231.                 (**doc).inGoAwayProc    = nil;
  232.                 (**doc).inZoomProc        = HandleZoomClick;
  233.                 (**doc).inGrowProc        = HandleGrow;
  234.                 (**doc).keyProc            = nil;
  235.                 (**doc).activateProc    = DoActivate;
  236.                 (**doc).updateProc        = DrawWindow;    
  237.                 (**doc).hScroll         = nil;
  238.                 (**doc).vScroll            = nil;
  239.                 (**doc).world            = nil;
  240.                 (**doc).pict            = nil;
  241. //                (**doc).printer            = nil;
  242.                 (**doc).dirty            = false;
  243.                 
  244.                 break;
  245.                 
  246.             case kDialogKind:
  247.                 break;
  248.  
  249.             case kAboutKind:
  250.                 (**doc).idleProc        = DoIdle;
  251.                 (**doc).mMenuProc        = HandleMenuChoice;
  252.                 (**doc).inContentProc    = nil;
  253.                 (**doc).inGoAwayProc    = nil;
  254.                 (**doc).inZoomProc        = nil;
  255.                 (**doc).inGrowProc        = nil;
  256.                 (**doc).keyProc            = nil;
  257.                 (**doc).activateProc    = nil;
  258.                 (**doc).updateProc        = DrawAboutWindow;    
  259.                 (**doc).hScroll         = nil;
  260.                 (**doc).vScroll            = nil;
  261.                 (**doc).world            = nil;
  262.                 (**doc).pict            = nil;
  263. //                (**doc).printer            = nil;
  264.                 (**doc).dirty            = false;
  265.                 
  266.                 break;
  267.                 
  268.             default:
  269.                 err = 25;
  270.                 break;    
  271.         }
  272.         ((WindowPeek)window)->windowKind = windKind;
  273.  
  274.     }            
  275.     return err;
  276.  
  277. }
  278.  
  279.  
  280.         
  281. //----------------------------------------------------------------------
  282. //
  283. //    DrawWindow - custom proc that is called to update window contents.
  284. //                 
  285. //
  286. //----------------------------------------------------------------------
  287.  
  288. void DrawWindow(WindowRef window, void *refCon)
  289. {
  290.     DocHnd            doc;
  291.     GWorldPtr        theWorld;
  292.     PixMapHandle    thePix;
  293.     Rect            cRect;
  294.     Rect            bounds;
  295.     
  296.     doc = (DocHnd)GetWRefCon(window);    
  297.     if (doc != nil) 
  298.     {
  299.     }
  300.     
  301. }
  302.  
  303.  
  304. //----------------------------------------------------------------------
  305. //
  306. //    DrawAboutWindow - custom proc that is called to update about window.
  307. //                 
  308. //
  309. //----------------------------------------------------------------------
  310.  
  311. void DrawAboutWindow( WindowRef window, void *refCon )
  312. {    
  313.     CGrafPtr        oldPort;
  314.     GDHandle        oldGD;
  315.     PicHandle        thePict;
  316.     
  317.     GetGWorld(&oldPort, &oldGD);
  318.     
  319.     thePict = GetPicture(rAboutPictID);
  320.     
  321.     if (thePict != nil)
  322.     {
  323.         SetGWorld((CGrafPtr)window, nil);
  324.         
  325.         DrawPicture(thePict, &window->portRect);
  326.     }
  327.     
  328.     SetGWorld(oldPort, oldGD);
  329.  
  330. }
  331.  
  332.  
  333. //----------------------------------------------------------------------
  334. //
  335. //    DoResizeWindow - custom proc that is called to update window.
  336. //                 
  337. //
  338. //----------------------------------------------------------------------
  339.  
  340. void DoResizeWindow (WindowRef window) 
  341. {
  342.  
  343. }
  344.  
  345.  
  346. //----------------------------------------------------------------------
  347. //
  348. //    GetWindKind - returns the windowkind.
  349. //                 
  350. //
  351. //----------------------------------------------------------------------
  352.  
  353. short GetWindKind(WindowRef window)
  354. {
  355.  
  356.     return ((WindowPeek)window)->windowKind;
  357.  
  358. }
  359.  
  360.  
  361. //----------------------------------------------------------------------
  362. //
  363. //    GetIsAppWindow - is the window a 'userKind'.
  364. //                 
  365. //
  366. //----------------------------------------------------------------------
  367.  
  368. Boolean GetIsAppWindow(WindowRef window)
  369. {
  370.     return (GetWindKind(window) == kDocKind);
  371.  
  372. }
  373.  
  374.  
  375. //----------------------------------------------------------------------
  376. //
  377. //    GetIsAboutWindow - is the window an about box.
  378. //                 
  379. //
  380. //----------------------------------------------------------------------
  381.  
  382. Boolean GetIsAboutWindow(WindowRef window)
  383. {
  384.     return (GetWindKind(window) == kAboutKind);
  385.     
  386. }
  387.